﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Ssb
{
    public class Hitbox
        : Ssb.Logic.Bubble
    {
        #region Properties
        /// <summary>
        /// The Offset is the distance the hitbox is moved from the center of the fighter's current position.
        /// </summary>
        public Vector2 Offset { get; protected set; }

        public float Damage { get; protected set; }

        /// <summary>
        /// An amount randomly added/subtracted from the Damage to simulate actual damage.
        /// </summary>
        public float DamageVariance { get; protected set; }

        /// <summary>
        /// The amount of pixels the target is initially projected away from the hitbox.
        /// </summary>
        public float KnockbackBase { get; protected set; }

        /// <summary>
        /// The amount subtracted from the Knockback to simulate air friction.
        /// </summary>
        public float KnockbackDegen { get; protected set; }

        /// <summary>
        /// The direction in which the target is projected.
        /// </summary>
        public float ProjectionAngle { get; protected set; }

        /// <summary>
        /// A give or take value added/subtracted from the ProjectionAngle to simulate actual projection.
        /// </summary>
        public float ProjectionAngleVariance { get; protected set; }

        /// <summary>
        /// The amount of frames the Hitbox lives for.
        /// </summary>
        public int LifeSpan { get; protected set; }

        /// <summary>
        /// The amount of frames to wait before activating the hitbox
        /// </summary>
        public int Delay { get; protected set; }
        #endregion

        #region Fields
        bool enabled;
        bool Enabled2;
        int delay;
        int lifespan;
        #endregion

        /// <summary>
        /// Creates a hitbox with set properties. 
        /// A hitbox is an area in which targets take damage and knockback.
        /// </summary>
        /// <param name="radius">The size of the hitbox</param>
        /// <param name="offset">The offset of te hitbox</param>
        /// <param name="damage">The initial damage dealt by the hitbox</param>
        /// <param name="damageVariance">A value added/subtracted to/from the damage</param>
        /// <param name="kbBase">The initial knockback of the hitbox</param>
        /// <param name="kbDegen">A value subtracted from the knockback on every frame</param>
        /// <param name="projectionAngle">The direction in which the target is launched</param>
        /// <param name="paVariance">A value added/subtracted to/from the angle</param>
        /// <param name="lifeSpan">The amount of frames the hitbox lasts</param>
        public Hitbox(int radius, Vector2 offset, float damage, float damageVariance, float kbBase, float kbDegen, float projectionAngle, float paVariance, int lifeSpan, int delay, Game game)
            : base(offset, radius, game)
        {
            Offset = new Vector2(offset.X, offset.Y);
            Damage = damage;
            DamageVariance = damageVariance;
            KnockbackBase = kbBase;
            KnockbackDegen = kbDegen;
            ProjectionAngle = projectionAngle;
            ProjectionAngleVariance = paVariance;

            Reset(); // To make the hitbox disabled, yet ready
        }

        public void Update()
        {
            if (!Enabled2)
                return;

            if (!enabled && lifespan <= 0)
                return;

            if (enabled)
            {
                if (lifespan <= 0)
                    enabled = false;
                lifespan--;
            }
            else
            {
                if (delay <= 0)
                    enabled = true;
                delay--;
            }
        }

        #region Methods
        /// <summary>
        /// Resets the hitbox for use again later. Is auto-called at the beginning of the FighterAnimation
        /// attached to the hitbox.
        /// </summary>
        public void Reset()
        {
            delay = Delay;
            lifespan = LifeSpan;
            enabled = false;
            Enabled2 = false;
        }

        /// <summary>
        /// Enables the hitbox making it's delay/lifespan decrese to the point where is has an effect.
        /// </summary>
        public void Enable()
        {
            Enabled2 = true;
        }
        #endregion
    }
}
